home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 2.iso / toolbox / public / figlet / figlet2.1.1 / figlet.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  34KB  |  1,315 lines

  1. /****************************************************************************
  2.  
  3.   figlet (C) 1991, 1993, 1994 Glenn Chappell and Ian Chai
  4.   Internet: <ggc@uiuc.edu> and <chai@uiuc.edu>
  5.   figlet, along with the various figlet fonts and documentation, may be
  6.     freely copied and distributed.
  7.   If you use figlet, please send an e-mail message to <figlet@uiuc.edu>.
  8.  
  9. ****************************************************************************/
  10.  
  11. #define DATE "28 Apr 1995"
  12. #define VERSION "2.1.1"
  13. #define VERSION_INT 20101
  14.  
  15. /* figlet (Frank, Ian & Glenn's Letters) */
  16. /* by Glenn Chappell */
  17. /* Apr 1991 */
  18. /* Automatic file addition by Ian Chai May 1991 */
  19. /* Punctuation and numbers addition by Ian Chai Jan 1993 */
  20. /* Full ASCII by Glenn Chappell Feb 1993 */
  21. /* Line-breaking, general rewrite by Glenn Chappell Mar 1993 */
  22. /* Hard blanks by Glenn Chappell Apr 1993 */
  23. /* Release 2.0 5 Aug 1993 */
  24. /* Right-to-left printing, extended char set by Glenn Chappell Dec 1993 */
  25. /* Control files by Glenn Chappell Feb 1994 */
  26. /* Release 2.1 12 Aug 1994 */
  27. /* Release 2.1.1 25 Aug 1994 */
  28.  
  29. /*---------------------------------------------------------------------------
  30.   DEFAULTFONTDIR and DEFAULTFONTFILE should be defined in the Makefile.
  31.   DEFAULTFONTDIR is the full path name of the directory in which figlet
  32.     will search first for fonts (the ".flf" files).
  33.   DEFAULTFONTFILE is the filename of the font to be used if no other
  34.     is specified (standard.flf is recommended, but any other can be
  35.     used). This file should reside in the directory specified by
  36.     DEFAULTFONTDIR.
  37. ---------------------------------------------------------------------------*/
  38. #ifndef DEFAULTFONTDIR
  39. #define DEFAULTFONTDIR "/usr/games/lib/figlet.dir"
  40. #endif
  41. #ifndef DEFAULTFONTFILE
  42. #define DEFAULTFONTFILE "standard.flf"
  43. #endif
  44.  
  45. #include <stdio.h>
  46. #ifdef __STDC__
  47. #include <stdlib.h>
  48. #endif
  49. #include <string.h>
  50. #include <ctype.h>
  51. #include <fcntl.h>     /* Needed for get_columns */
  52. #include <sys/ioctl.h> /* Needed for get_columns */
  53.  
  54. #define MYSTRLEN(x) ((int)strlen(x)) /* Eliminate ANSI problem */
  55. #define DIRSEP '/' /* E.g., make this '\\' for an MS-DOS port. */
  56.                    /* Note: '/' also used in filename in get_columns(). */
  57.  
  58. #define FONTFILESUFFIX ".flf"
  59. #define FONTFILEMAGICNUMBER "flf2"
  60. #define FSUFFIXLEN MYSTRLEN(FONTFILESUFFIX)
  61. #define CONTROLFILESUFFIX ".flc"
  62. #define CONTROLFILEMAGICNUMBER "flc2"
  63. #define CSUFFIXLEN MYSTRLEN(CONTROLFILESUFFIX)
  64. #define DEFAULTCOLUMNS 80
  65.  
  66.  
  67. /****************************************************************************
  68.  
  69.   Globals dealing with chars that are read
  70.  
  71. ****************************************************************************/
  72.  
  73. typedef long inchr; /* "char" read from stdin */
  74.  
  75. inchr *inchrline;  /* Alloc'd inchr inchrline[inchrlinelenlimit+1]; */
  76.                    /* Note: not null-terminated. */
  77. int inchrlinelen,inchrlinelenlimit;
  78.  
  79.  
  80. /****************************************************************************
  81.  
  82.   Globals dealing with chars that are written
  83.  
  84. ****************************************************************************/
  85.  
  86. typedef struct fc {
  87.   inchr ord;
  88.   char **thechar;  /* Alloc'd char thechar[charheight][]; */
  89.   struct fc *next;
  90.   } fcharnode;
  91.  
  92. fcharnode *fcharlist;
  93. char **currchar;
  94. int currcharwidth;
  95. char **outline;    /* Alloc'd char outline[charheight][outlinelenlimit+1]; */
  96. int outlinelen;
  97.  
  98.  
  99. /****************************************************************************
  100.  
  101.   Globals dealing with command file storage
  102.  
  103. ****************************************************************************/
  104.  
  105. typedef struct cfn {
  106.   char *thename;
  107.   struct cfn *next;
  108.   } cfnamenode;
  109.  
  110. cfnamenode *cfilelist,**cfilelistend;
  111.  
  112. typedef struct cm {
  113.   int thecommand;
  114.   inchr rangelo;
  115.   inchr rangehi;
  116.   inchr offset;
  117.   struct cm *next;
  118.   } comnode;
  119.  
  120. comnode *commandlist,**commandlistend;
  121.  
  122. /****************************************************************************
  123.  
  124.   Globals affected by command line options
  125.  
  126. ****************************************************************************/
  127.  
  128. int deutschflag,justification,paragraphflag,right2left;
  129. /*---------------------------------------------------------------------------
  130.   smushmode: (given after "-m" command line switch)
  131.    -2: Get default value from font file (default)
  132.    -1: Do not smush
  133.   For explanation of non-negative values, see smushem().
  134. ---------------------------------------------------------------------------*/
  135. int smushmode;
  136. int outputwidth;
  137. int outlinelenlimit;
  138. char *fontdirname,*fontname;
  139.  
  140.  
  141. /****************************************************************************
  142.  
  143.   Globals read from font file
  144.  
  145. ****************************************************************************/
  146.  
  147. char hardblank;
  148. int charheight,defaultmode;
  149.  
  150.  
  151. /****************************************************************************
  152.  
  153.   Name of program, used in error messages
  154.  
  155. ****************************************************************************/
  156.  
  157. char *myname;
  158.  
  159.  
  160. #ifdef TIOCGWINSZ
  161. /****************************************************************************
  162.  
  163.   get_columns
  164.  
  165.   Determines the number of columns of /dev/tty.  Returns the number of
  166.   columns, or -1 if error.  May return 0 if columns unknown.
  167.   Requires include files <fcntl.h> and <sys/ioctl.h>.
  168.   by Glenn Chappell & Ian Chai 14 Apr 1993
  169.  
  170. ****************************************************************************/
  171.  
  172. int get_columns()
  173. {
  174.   struct winsize ws;
  175.   int fd,result;
  176.  
  177.   if ((fd = open("/dev/tty",O_WRONLY))<0) return -1;
  178.   result = ioctl(fd,TIOCGWINSZ,&ws);
  179.   close(fd);
  180.   return result?-1:ws.ws_col;
  181. }
  182. #endif /* ifdef TIOCGWINSZ */
  183.  
  184.  
  185. /****************************************************************************
  186.  
  187.   myalloc
  188.  
  189.   Calls malloc.  If malloc returns error, prints error message and
  190.   quits.
  191.  
  192. ****************************************************************************/
  193.  
  194. #ifdef __STDC__
  195. char *myalloc(size_t size)
  196. #else
  197. char *myalloc(size)
  198. int size;
  199. #endif
  200. {
  201.   char *ptr;
  202. #ifndef __STDC__
  203.   extern void *malloc();
  204. #endif
  205.  
  206.   if ((ptr = (char*)malloc(size))==NULL) {
  207.     fprintf(stderr,"%s: Out of memory\n",myname);
  208.     exit(1);
  209.     }
  210.   else {
  211.     return ptr;
  212.     }
  213. }
  214.  
  215.  
  216. /****************************************************************************
  217.  
  218.   skiptoeol
  219.  
  220.   Skips to the end of a line, given a stream.
  221.  
  222. ****************************************************************************/
  223.  
  224. void skiptoeol(fp)
  225. FILE *fp;
  226. {
  227.   int dummy;
  228.  
  229.   while (dummy=getc(fp),dummy!='\n'&&dummy!=EOF) ;
  230. }
  231.  
  232.  
  233. /****************************************************************************
  234.  
  235.   usageerr
  236.  
  237.   Prints "Usage: ...." line to the given stream.
  238.  
  239. ****************************************************************************/
  240.  
  241. void printusage(out)
  242. FILE *out;
  243. {
  244.   fprintf(out,
  245.     "Usage: %s [ -clnprtvxDELNRX ] [ -d fontdirectory ]\n",
  246.     myname);
  247.   fprintf(out,
  248.     "              [ -f fontfile ] [ -m smushmode ] [ -w outputwidth ]\n");
  249.   fprintf(out,
  250.     "              [ -C controlfile ] [ -I infocode ]\n");
  251. }
  252.  
  253.  
  254. /****************************************************************************
  255.  
  256.   printinfo
  257.  
  258.   Prints version and copyright message, or utility information.
  259.  
  260. ****************************************************************************/
  261.  
  262. void printinfo(infonum)
  263. int infonum;
  264. {
  265.   switch (infonum) {
  266.     case 0: /* Copyright message */
  267.       printf("figlet (C) 1991, 1993, 1994 Glenn Chappell and Ian Chai\n");
  268.       printf("Internet: <ggc@uiuc.edu> and <chai@uiuc.edu>\n");
  269.       printf("Version: %s, date: %s\n\n",VERSION,DATE);
  270.       printf("figlet, along with the various figlet fonts");
  271.       printf(" and documentation, may be\n");
  272.       printf("freely copied and distributed.\n\n");
  273.       printf("If you use figlet, please send an");
  274.       printf(" e-mail message to <figlet@uiuc.edu>.\n\n");
  275.       printf("The latest version of figlet is available by anonymous");
  276.       printf(" FTP from\n");
  277.       printf("ftp.nicoh.com in directory \"pub/figlet\".\n\n");
  278.       printusage(stdout);
  279.       break;
  280.     case 1: /* Version (integer) */
  281.       printf("%d\n",VERSION_INT);
  282.       break;
  283.     case 2: /* Font directory */
  284.       printf("%s\n",fontdirname);
  285.       break;
  286.     case 3: /* Font */
  287.       printf("%s\n",fontname);
  288.       break;
  289.     case 4: /* Outputwidth */
  290.       printf("%d\n",outputwidth);
  291.     }
  292. }
  293.  
  294.  
  295. /****************************************************************************
  296.  
  297.   readTchar
  298.  
  299.   Reads a control file "T" command character specification.
  300.  
  301. ****************************************************************************/
  302.  
  303. inchr readTchar(fp)
  304. FILE *fp;
  305. {
  306.   inchr thechar;
  307.   char next;
  308.  
  309.   thechar=getc(fp);
  310.   if (thechar=='\n') { /* Handle badly-formatted file */
  311.     ungetc(thechar,fp);
  312.     return '\0';
  313.     }
  314.   if (thechar!='\\') return thechar;
  315.   next=getc(fp);
  316.   switch(next) {
  317.     case 'a':
  318.       return 7;
  319.     case 'b':
  320.       return 8;
  321.     case 'e':
  322.       return 27;
  323.     case 'f':
  324.       return 12;
  325.     case 'n':
  326.       return 10;
  327.     case 'r':
  328.       return 13;
  329.     case 't':
  330.       return 9;
  331.     case 'v':
  332.       return 11;
  333.     default:
  334.       if (next=='-' || next=='x' || (next>='0' && next<='9')) {
  335.         ungetc(next,fp);
  336.         fscanf(fp,"%li",&thechar);
  337.         return thechar;
  338.         }
  339.       return next;
  340.     }
  341. }
  342.  
  343.  
  344. /****************************************************************************
  345.  
  346.   readcontrol
  347.  
  348.   Allocates memory and reads in the given control file.
  349.   Called in readcontrolfiles().
  350.  
  351. ****************************************************************************/
  352.  
  353. void readcontrol(controlname)
  354. char *controlname;
  355. {
  356.   inchr firstch,lastch;
  357.   char dashcheck;
  358.   inchr offset;
  359.   char *controlpath,magicnum[5];
  360.   char command;
  361.   FILE *controlfile;
  362.   int namelen;
  363.  
  364.   namelen = MYSTRLEN(fontdirname);
  365.   controlpath = (char*)myalloc(sizeof(char)
  366.     *(namelen+MYSTRLEN(controlname)+CSUFFIXLEN+2));
  367.   controlfile = NULL;
  368.   if (!strchr(controlname,DIRSEP)) {
  369.     strcpy(controlpath,fontdirname);
  370.     controlpath[namelen] = DIRSEP;
  371.     controlpath[namelen+1] = '\0';
  372.     strcat(controlpath,controlname);
  373.     strcat(controlpath,CONTROLFILESUFFIX);
  374.     controlfile = fopen(controlpath,"r");
  375.     }
  376.   if (controlfile==NULL) {
  377.     strcpy(controlpath,controlname);
  378.     strcat(controlpath,CONTROLFILESUFFIX);
  379.     controlfile = fopen(controlpath,"r");
  380.     if (controlfile==NULL) {
  381.       fprintf(stderr,"%s: %s: Unable to open control file\n",myname,
  382.         controlpath);
  383.       exit(1);
  384.       }
  385.     }
  386.  
  387.   fscanf(controlfile,"%4s",magicnum);
  388.   if (strcmp(magicnum,CONTROLFILEMAGICNUMBER)) {
  389.     fprintf(stderr,"%s: %s: Not a figlet 2 control file\n",myname,controlpath);
  390.     exit(1);
  391.     }
  392.   skiptoeol(controlfile);
  393.   free(controlpath);
  394.  
  395.   (*commandlistend) = (comnode*)myalloc(sizeof(comnode));
  396.   (*commandlistend)->thecommand = 0; /* Begin with a freeze command */
  397.   commandlistend = &(*commandlistend)->next;
  398.   (*commandlistend) = NULL;
  399.  
  400.   while(command=getc(controlfile),!feof(controlfile)) {
  401.     switch (command) {
  402.       case 't': /* Translate */
  403.         fscanf(controlfile,"%*[ \t]");
  404.         firstch=readTchar(controlfile);
  405.         if ((dashcheck=getc(controlfile))=='-') {
  406.           lastch=readTchar(controlfile);
  407.           }
  408.         else {
  409.           ungetc(dashcheck,controlfile);
  410.           lastch=firstch;
  411.           }
  412.         fscanf(controlfile,"%*[ \t]");
  413.         offset=readTchar(controlfile)-firstch;
  414.         skiptoeol(controlfile);
  415.         (*commandlistend) = (comnode*)myalloc(sizeof(comnode));
  416.         (*commandlistend)->thecommand = 1;
  417.         (*commandlistend)->rangelo = firstch;
  418.         (*commandlistend)->rangehi = lastch;
  419.         (*commandlistend)->offset = offset;
  420.         commandlistend = &(*commandlistend)->next;
  421.         (*commandlistend) = NULL;
  422.         break;
  423.       case 'f': /* freeze */
  424.         skiptoeol(controlfile);
  425.         (*commandlistend) = (comnode*)myalloc(sizeof(comnode));
  426.         (*commandlistend)->thecommand = 0;
  427.         commandlistend = &(*commandlistend)->next;
  428.         (*commandlistend) = NULL;
  429.         break;
  430.       case '\n': /* blank line */
  431.         break;
  432.       default: /* Includes '#' */
  433.         skiptoeol(controlfile);
  434.       }
  435.     }
  436.   fclose(controlfile);
  437. }
  438.  
  439.  
  440. /****************************************************************************
  441.  
  442.   readcontrolfiles
  443.  
  444.   Reads in the controlfiles names in cfilelist.  Uses readcontrol.
  445.   Called in main().
  446.  
  447. ****************************************************************************/
  448.  
  449. void readcontrolfiles()
  450. {
  451.   cfnamenode *cfnptr;
  452.  
  453.   for (cfnptr=cfilelist;cfnptr!=NULL;cfnptr=cfnptr->next) {
  454.     readcontrol(cfnptr->thename);
  455.     }
  456. }
  457.  
  458.  
  459. /****************************************************************************
  460.  
  461.   clearcfilelist
  462.  
  463.   Clears the control file list.  Assumes thename does not need freeing.
  464.  
  465. ****************************************************************************/
  466.  
  467. void clearcfilelist()
  468. {
  469.   cfnamenode *cfnptr1,*cfnptr2;
  470.  
  471.   cfnptr1 = cfilelist;
  472.   while (cfnptr1 != NULL) {
  473.     cfnptr2 = cfnptr1->next;
  474.     free(cfnptr1);
  475.     cfnptr1 = cfnptr2;
  476.     }
  477.   cfilelist = NULL;
  478.   cfilelistend = &cfilelist;
  479. }
  480.  
  481.  
  482. /****************************************************************************
  483.  
  484.   getparams
  485.  
  486.   Handles all command-line parameters.  Puts all parameters within
  487.   bounds except smushmode.
  488.  
  489. ****************************************************************************/
  490.  
  491. void getparams(argc,argv)
  492. int argc;
  493. char *argv[];
  494. {
  495.   extern char *optarg;
  496.   extern int optind;
  497.   int c; /* "Should" be a char -- need int for "!= -1" test*/
  498.   int columns,firstfont,infoprint;
  499.   char *controlname;
  500.  
  501.   if ((myname = strrchr(argv[0],DIRSEP))!=NULL) {
  502.     myname++;
  503.     }
  504.   else {
  505.     myname = argv[0];
  506.     }
  507.   fontdirname = DEFAULTFONTDIR;
  508.   firstfont = 1;
  509.   fontname = (char*)myalloc(sizeof(char)*(MYSTRLEN(DEFAULTFONTFILE)+1));
  510.   strcpy(fontname,DEFAULTFONTFILE); /* Some systems don't have strdup() */
  511.   if ((MYSTRLEN(fontname)>=FSUFFIXLEN)?
  512.     !strcmp(fontname+MYSTRLEN(fontname)-FSUFFIXLEN,FONTFILESUFFIX):0) {
  513.     fontname[MYSTRLEN(fontname)-FSUFFIXLEN]='\0';
  514.     }
  515.   cfilelist = NULL;
  516.   cfilelistend = &cfilelist;
  517.   commandlist = NULL;
  518.   commandlistend = &commandlist;
  519.   smushmode = -2;
  520.   deutschflag = 0;
  521.   justification = -1;
  522.   right2left = -1;
  523.   paragraphflag = 0;
  524.   infoprint = -1;
  525.   outputwidth = DEFAULTCOLUMNS;
  526.   while ((c = getopt(argc,argv,"DEXLRI:xlcrpntvm:w:d:f:C:NF"))!= -1) {
  527.       /* Note: -F is not a legal option -- prints a special err message.  */
  528.     switch (c) {
  529.       case 'D':
  530.         deutschflag = 1;
  531.         break;
  532.       case 'E':
  533.         deutschflag = 0;
  534.         break;
  535.       case 'X':
  536.         right2left = -1;
  537.         break;
  538.       case 'L':
  539.         right2left = 0;
  540.         break;
  541.       case 'R':
  542.         right2left = 1;
  543.         break;
  544.       case 'x':
  545.         justification = -1;
  546.         break;
  547.       case 'l':
  548.         justification = 0;
  549.         break;
  550.       case 'c':
  551.         justification = 1;
  552.         break;
  553.       case 'r':
  554.         justification = 2;
  555.         break;
  556.       case 'p':
  557.         paragraphflag = 1;
  558.         break;
  559.       case 'n':
  560.         paragraphflag = 0;
  561.         break;
  562.       case 't':
  563. #ifdef TIOCGWINSZ
  564.         columns = get_columns();
  565.         if (columns>0) {
  566.           outputwidth = columns;
  567.           }
  568. #else /* ifdef TIOCGWINSZ */
  569.         fprintf(stderr,
  570.           "%s: \"-t\" is disabled, since ioctl is not fully implemented.\n",
  571.           myname);
  572. #endif /* ifdef TIOCGWINSZ */
  573.         break;
  574.       case 'v':
  575.         infoprint = 0;
  576.         break;
  577.       case 'I':
  578.         infoprint = atoi(optarg);
  579.         break;
  580.       case 'm':
  581.         smushmode = atoi(optarg);
  582.         /* Putting smushmode within bounds is done in readfont(). */
  583.         break;
  584.       case 'w':
  585.         columns = atoi(optarg);
  586.         if (columns>0) {
  587.           outputwidth = columns;
  588.           }
  589.         break;
  590.       case 'd':
  591.         fontdirname = optarg;
  592.         break;
  593.       case 'f':
  594.         if (firstfont) {
  595.           free(fontname);
  596.           firstfont = 0;
  597.           }
  598.         fontname = optarg;
  599.         if ((MYSTRLEN(fontname)>=FSUFFIXLEN)?
  600.           !strcmp(fontname+MYSTRLEN(fontname)-FSUFFIXLEN,FONTFILESUFFIX):0) {
  601.           fontname[MYSTRLEN(fontname)-FSUFFIXLEN] = '\0';
  602.           }
  603.         break;
  604.       case 'C':
  605.         controlname = optarg;
  606.         if ((MYSTRLEN(controlname)>=CSUFFIXLEN)?
  607.           !strcmp(controlname+MYSTRLEN(controlname)-CSUFFIXLEN,
  608.             CONTROLFILESUFFIX):0) {
  609.           controlname[MYSTRLEN(controlname)-CSUFFIXLEN] = '\0';
  610.           }
  611.         (*cfilelistend) = (cfnamenode*)myalloc(sizeof(cfnamenode));
  612.         (*cfilelistend)->thename = controlname;
  613.         cfilelistend = &(*cfilelistend)->next;
  614.         (*cfilelistend) = NULL;
  615.         break;
  616.       case 'N':
  617.         clearcfilelist();
  618.         break;
  619.       case 'F': /* Not a legal option */
  620.         fprintf(stderr,"%s: illegal option -- F\n",myname);
  621.         printusage(stderr);
  622.         fprintf(stderr,"\nBecause of numerous incompatibilities, the");
  623.         fprintf(stderr," \"-F\" option has been\n");
  624.         fprintf(stderr,"removed.  It has been replaced by the \"figlist\"");
  625.         fprintf(stderr," program, which is now\n");
  626.         fprintf(stderr,"included in the basic figlet package.  \"figlist\"");
  627.         fprintf(stderr," is also available by\n");
  628.         fprintf(stderr,"anonymous FTP from ftp.nicoh.com in");
  629.         fprintf(stderr," directory \"pub/figlet/util\".\n");
  630.         exit(1);
  631.         break;
  632.       default:
  633.         printusage(stderr);
  634.         exit(1);
  635.       }
  636.     }
  637.   if (optind!=argc) {
  638.     printusage(stderr);
  639.     exit(1);
  640.     }
  641.   outlinelenlimit = outputwidth-1;
  642.   if (infoprint>=0) {
  643.     printinfo(infoprint);
  644.     exit(0);
  645.     }
  646. }
  647.  
  648.  
  649. /****************************************************************************
  650.  
  651.   clearline
  652.  
  653.   Clears both the input (inchrline) and output (outline) storage.
  654.  
  655. ****************************************************************************/
  656.  
  657. void clearline()
  658. {
  659.   int i;
  660.  
  661.   for (i=0;i<charheight;i++) {
  662.     outline[i][0] = '\0';
  663.     }
  664.   outlinelen = 0;
  665.   inchrlinelen = 0;
  666. }
  667.  
  668.  
  669. /****************************************************************************
  670.  
  671.   readfontchar
  672.  
  673.   Reads a font character from the font file, and places it in a
  674.   newly-allocated entry in the list.
  675.  
  676. ****************************************************************************/
  677.  
  678. void readfontchar(file,theord,line,maxlen)
  679. FILE *file;
  680. inchr theord;
  681. char *line;
  682. int maxlen;
  683. {
  684.   int row,k;
  685.   char endchar;
  686.   fcharnode *fclsave;
  687.  
  688.   fclsave = fcharlist;
  689.   fcharlist = (fcharnode*)myalloc(sizeof(fcharnode));
  690.   fcharlist->ord = theord;
  691.   fcharlist->thechar = (char**)myalloc(sizeof(char*)*charheight);
  692.   fcharlist->next = fclsave;
  693.   for (row=0;row<charheight;row++) {
  694.     if (fgets(line,maxlen+1,file)==NULL) {
  695.       line[0] = '\0';
  696.       }
  697.     k = MYSTRLEN(line)-1;
  698.     while (k>=0 && isspace(line[k])) {
  699.       k--;
  700.       }
  701.     if (k>=0) {
  702.       endchar = line[k];
  703.       while (k>=0 ? line[k]==endchar : 0) {
  704.         k--;
  705.         }
  706.       }
  707.     line[k+1] = '\0';
  708.     fcharlist->thechar[row] = (char*)myalloc(sizeof(char)*(k+2));
  709.     strcpy(fcharlist->thechar[row],line);
  710.     }
  711. }
  712.  
  713.  
  714. /****************************************************************************
  715.  
  716.   readfont
  717.  
  718.   Allocates memory, initializes variables, and reads in the font.
  719.   Called near beginning of main().
  720.  
  721. ****************************************************************************/
  722.  
  723. void readfont()
  724. {
  725. #define MAXFIRSTLINELEN 1000
  726.   int i,row,numsread;
  727.   inchr theord;
  728.   int maxlen,cmtlines,ffright2left;
  729.   char *fontpath,*fileline,magicnum[5];
  730.   FILE *fontfile;
  731.   int namelen;
  732.  
  733.   namelen = MYSTRLEN(fontdirname);
  734.   fontpath = (char*)myalloc(sizeof(char)
  735.     *(namelen+MYSTRLEN(fontname)+FSUFFIXLEN+2));
  736.   fontfile = NULL;
  737.   if (!strchr(fontname,DIRSEP)) {
  738.     strcpy(fontpath,fontdirname);
  739.     fontpath[namelen] = DIRSEP;
  740.     fontpath[namelen+1] = '\0';
  741.     strcat(fontpath,fontname);
  742.     strcat(fontpath,FONTFILESUFFIX);
  743.     fontfile = fopen(fontpath,"r");
  744.     }
  745.   if (fontfile==NULL) {
  746.     strcpy(fontpath,fontname);
  747.     strcat(fontpath,FONTFILESUFFIX);
  748.     fontfile = fopen(fontpath,"r");
  749.     if (fontfile==NULL) {
  750.       fprintf(stderr,"%s: %s: Unable to open font file\n",myname,fontpath);
  751.       exit(1);
  752.       }
  753.     }
  754.  
  755.   fscanf(fontfile,"%4s",magicnum);
  756.   fileline = (char*)myalloc(sizeof(char)*(MAXFIRSTLINELEN+1));
  757.   if (fgets(fileline,MAXFIRSTLINELEN+1,fontfile)==NULL) {
  758.     fileline[0] = '\0';
  759.     }
  760.   if (MYSTRLEN(fileline)>0 ? fileline[MYSTRLEN(fileline)-1]!='\n' : 0) {
  761.     skiptoeol(stdin);
  762.     }
  763.   numsread = sscanf(fileline,"%*c%c %d %*d %d %d %d%*[ \t]%d",
  764.     &hardblank,&charheight,&maxlen,&defaultmode,&cmtlines,
  765.     &ffright2left);
  766.   free(fileline);
  767.   if (strcmp(magicnum,FONTFILEMAGICNUMBER) || numsread<5) {
  768.     fprintf(stderr,"%s: %s: Not a figlet 2 font file\n",myname,fontpath);
  769.     exit(1);
  770.     }
  771.   for (i=1;i<=cmtlines;i++) {
  772.     skiptoeol(fontfile);
  773.     }
  774.   free(fontpath);
  775.  
  776.   if (numsread<6) {
  777.     ffright2left = 0;
  778.     }
  779.   if (charheight<1) {
  780.     charheight = 1;
  781.     }
  782.   if (maxlen<1) {
  783.     maxlen = 1;
  784.     }
  785.   maxlen += 100; /* Give ourselves some extra room */
  786.   if (smushmode<-1) {
  787.     smushmode = defaultmode;
  788.     }
  789.   if (smushmode<-1) {
  790.     smushmode = -1;
  791.     }
  792.   if (right2left<0) {
  793.     right2left = ffright2left;
  794.     }
  795.   if (justification<0) {
  796.     justification = 2*right2left;
  797.     }
  798.  
  799.   fileline = (char*)myalloc(sizeof(char)*(maxlen+1));
  800.   /* Allocate "missing" character */
  801.   fcharlist = (fcharnode*)myalloc(sizeof(fcharnode));
  802.   fcharlist->ord = 0;
  803.   fcharlist->thechar = (char**)myalloc(sizeof(char*)*charheight);
  804.   fcharlist->next = NULL;
  805.   for (row=0;row<charheight;row++) {
  806.     fcharlist->thechar[row] = (char*)myalloc(sizeof(char));
  807.     fcharlist->thechar[row][0] = '\0';
  808.     }
  809.   for (theord=' ';theord<='~';theord++) {
  810.     readfontchar(fontfile,theord,fileline,maxlen);
  811.     }
  812.   for (theord= -255;theord<= -249;theord++) {
  813.     readfontchar(fontfile,theord,fileline,maxlen);
  814.     }
  815.   while (fgets(fileline,maxlen+1,fontfile)==NULL?0:
  816.     sscanf(fileline,"%li",&theord)==1) {
  817.     readfontchar(fontfile,theord,fileline,maxlen);
  818.     }
  819.   fclose(fontfile);
  820.   free(fileline);
  821. }
  822.  
  823.  
  824. /****************************************************************************
  825.  
  826.   linealloc
  827.  
  828.   Allocates & clears outline, inchrline. Sets inchrlinelenlimit.
  829.   Called near beginning of main().
  830.  
  831. ****************************************************************************/
  832.  
  833. void linealloc()
  834. {
  835.   int row; 
  836.  
  837.   outline = (char**)myalloc(sizeof(char*)*charheight);
  838.   for (row=0;row<charheight;row++) {
  839.     outline[row] = (char*)myalloc(sizeof(char)*(outlinelenlimit+1));
  840.     }
  841.   inchrlinelenlimit = outputwidth*4+100;
  842.   inchrline = (inchr*)myalloc(sizeof(inchr)*(inchrlinelenlimit+1));
  843.   clearline();
  844. }
  845.  
  846.  
  847. /****************************************************************************
  848.  
  849.   getletter
  850.  
  851.   Sets currchar to point to the font entry for the given character.
  852.   Sets currcharwidth to the width of this character.
  853.  
  854. ****************************************************************************/
  855.  
  856. void getletter(c)
  857. inchr c;
  858. {
  859.   fcharnode *charptr;
  860.  
  861.   for (charptr=fcharlist;charptr==NULL?0:charptr->ord!=c;
  862.     charptr=charptr->next) ;
  863.   if (charptr!=NULL) {
  864.     currchar = charptr->thechar;
  865.     }
  866.   else {
  867.     for (charptr=fcharlist;charptr==NULL?0:charptr->ord!=0;
  868.       charptr=charptr->next) ;
  869.     currchar = charptr->thechar;
  870.     }
  871.   currcharwidth = MYSTRLEN(currchar[0]);
  872. }
  873.  
  874.  
  875. /****************************************************************************
  876.  
  877.   smushem
  878.  
  879.   Given 2 characters, attempts to smush them into 1, according to
  880.   smushmode.  Returns smushed character or '\0' if no smushing can be
  881.   done.  Assumes smushmode >= 0.
  882.  
  883.   smushmode values are sum of following (all values smush blanks):
  884.     1: Smush equal chars (not hardblanks)
  885.     2: Smush '_' with any char in hierarchy below
  886.     4: hierarchy: "|", "/\", "[]", "{}", "()", "<>"
  887.        Each class in hier. can be replaced by later class.
  888.     8: [ + ] -> |, { + } -> |, ( + ) -> |
  889.    16: / + \ -> X, > + < -> X (only in that order)
  890.    32: hardblank + hardblank -> hardblank
  891.  
  892. ****************************************************************************/
  893.  
  894. char smushem(lch,rch)
  895. char lch,rch;
  896. {
  897.   if (lch==' ') return rch;
  898.   if (rch==' ') return lch;
  899.  
  900.   if (smushmode & 32) {
  901.     if (lch==hardblank && rch==hardblank) return lch;
  902.     }
  903.  
  904.   if (lch==hardblank || rch==hardblank) return '\0';
  905.  
  906.   if (smushmode & 1) {
  907.     if (lch==rch) return lch;
  908.     }
  909.  
  910.   if (smushmode & 2) {
  911.     if (lch=='_' && strchr("|/\\[]{}()<>",rch)) return rch;
  912.     if (rch=='_' && strchr("|/\\[]{}()<>",lch)) return lch;
  913.     }
  914.  
  915.   if (smushmode & 4) {
  916.     if (lch=='|' && strchr("/\\[]{}()<>",rch)) return rch;
  917.     if (rch=='|' && strchr("/\\[]{}()<>",lch)) return lch;
  918.     if (strchr("/\\",lch) && strchr("[]{}()<>",rch)) return rch;
  919.     if (strchr("/\\",rch) && strchr("[]{}()<>",lch)) return lch;
  920.     if (strchr("[]",lch) && strchr("{}()<>",rch)) return rch;
  921.     if (strchr("[]",rch) && strchr("{}()<>",lch)) return lch;
  922.     if (strchr("{}",lch) && strchr("()<>",rch)) return rch;
  923.     if (strchr("{}",rch) && strchr("()<>",lch)) return lch;
  924.     if (strchr("()",lch) && strchr("<>",rch)) return rch;
  925.     if (strchr("()",rch) && strchr("<>",lch)) return lch;
  926.     }
  927.  
  928.   if (smushmode & 8) {
  929.     if (lch=='[' && rch==']') return '|';
  930.     if (rch=='[' && lch==']') return '|';
  931.     if (lch=='{' && rch=='}') return '|';
  932.     if (rch=='{' && lch=='}') return '|';
  933.     if (lch=='(' && rch==')') return '|';
  934.     if (rch=='(' && lch==')') return '|';
  935.     }
  936.  
  937.   if (smushmode & 16) {
  938.     if (lch=='/' && rch=='\\') return 'X';
  939.     if (rch=='/' && lch=='\\') return 'X';
  940.     if (lch=='>' && rch=='<') return 'X';
  941.       /* Don't want the reverse of above to give 'X'. */
  942.     }
  943.  
  944.   return '\0';
  945. }
  946.  
  947.  
  948. /****************************************************************************
  949.  
  950.   smushamt
  951.  
  952.   Returns the maximum amount that the current character can be smushed
  953.   into the current line.
  954.  
  955. ****************************************************************************/
  956.  
  957. int smushamt()
  958. {
  959.   int maxsmush,amt;
  960.   int row,linebd,charbd;
  961.   char ch1,ch2;
  962.  
  963.   if (smushmode== -1) {
  964.     return 0;
  965.     }
  966.   maxsmush = currcharwidth;
  967.   for (row=0;row<charheight;row++) {
  968.     if (right2left) {
  969.       for (charbd=MYSTRLEN(currchar[row]);
  970.         ch1=currchar[row][charbd],(charbd>0&&(!ch1||ch1==' '));charbd--) ;
  971.       for (linebd=0;ch2=outline[row][linebd],ch2==' ';linebd++) ;
  972.       amt = linebd+currcharwidth-1-charbd;
  973.       }
  974.     else {
  975.       for (linebd=MYSTRLEN(outline[row]);
  976.         ch1 = outline[row][linebd],(linebd>0&&(!ch1||ch1==' '));linebd--) ;
  977.       for (charbd=0;ch2=currchar[row][charbd],ch2==' ';charbd++) ;
  978.       amt = charbd+outlinelen-1-linebd;
  979.       }
  980.     if (!ch1||ch1==' ') {
  981.       amt++;
  982.       }
  983.     else if (ch2) {
  984.       if (smushem(ch1,ch2)!='\0') {
  985.         amt++;
  986.         }
  987.       }
  988.     if (amt<maxsmush) {
  989.       maxsmush = amt;
  990.       }
  991.     }
  992.   return maxsmush;
  993. }
  994.  
  995.  
  996. /****************************************************************************
  997.  
  998.   addchar
  999.  
  1000.   Attempts to add the given character onto the end of the current line.
  1001.   Returns 1 if this can be done, 0 otherwise.
  1002.  
  1003. ****************************************************************************/
  1004.  
  1005. int addchar(c)
  1006. inchr c;
  1007. {
  1008.   int smushamount,row,k;
  1009.   char *templine;
  1010.  
  1011.   getletter(c);
  1012.   smushamount = smushamt();
  1013.   if (outlinelen+currcharwidth-smushamount>outlinelenlimit
  1014.       ||inchrlinelen+1>inchrlinelenlimit) {
  1015.     return 0;
  1016.     }
  1017.  
  1018.   templine = (char*)myalloc(sizeof(char)*(outlinelenlimit+1));
  1019.   for (row=0;row<charheight;row++) {
  1020.     if (right2left) {
  1021.       strcpy(templine,currchar[row]);
  1022.       for (k=0;k<smushamount;k++) {
  1023.         templine[currcharwidth-smushamount+k] =
  1024.           smushem(templine[currcharwidth-smushamount+k],outline[row][k]);
  1025.         }
  1026.       strcat(templine,outline[row]+smushamount);
  1027.       strcpy(outline[row],templine);
  1028.       }
  1029.     else {
  1030.       for (k=0;k<smushamount;k++) {
  1031.         outline[row][outlinelen-smushamount+k] =
  1032.           smushem(outline[row][outlinelen-smushamount+k],currchar[row][k]);
  1033.         }
  1034.       strcat(outline[row],currchar[row]+smushamount);
  1035.       }
  1036.     }
  1037.   free(templine);
  1038.   outlinelen = MYSTRLEN(outline[0]);
  1039.   inchrline[inchrlinelen++] = c;
  1040.   return 1;
  1041. }
  1042.  
  1043.  
  1044. /****************************************************************************
  1045.  
  1046.   putstring
  1047.  
  1048.   Prints out the given null-terminated string, substituting blanks
  1049.   for hardblanks.  If outputwidth is 1, prints the entire string;
  1050.   otherwise prints at most outputwidth-1 characters.  Prints a newline
  1051.   at the end of the string.  The string is left-justified, centered or
  1052.   right-justified (taking outputwidth as the screen width) if
  1053.   justification is 0, 1 or 2, respectively.
  1054.  
  1055. ****************************************************************************/
  1056.  
  1057. void putstring(string)
  1058. char *string;
  1059. {
  1060.   int i,len;
  1061.  
  1062.   len = MYSTRLEN(string);
  1063.   if (outputwidth>1) {
  1064.     if (len>outputwidth-1) {
  1065.       len = outputwidth-1;
  1066.       }
  1067.     if (justification>0) {
  1068.       for (i=1;(3-justification)*i+len+justification-2<outputwidth;i++) {
  1069.         putchar(' ');
  1070.         }
  1071.       }
  1072.     }
  1073.   for (i=0;i<len;i++) {
  1074.     putchar(string[i]==hardblank?' ':string[i]);
  1075.     }
  1076.   putchar('\n');
  1077. }
  1078.  
  1079.  
  1080. /****************************************************************************
  1081.  
  1082.   printline
  1083.  
  1084.   Prints outline using putstring, then clears the current line.
  1085.  
  1086. ****************************************************************************/
  1087.  
  1088. void printline()
  1089. {
  1090.   int i;
  1091.  
  1092.   for (i=0;i<charheight;i++) {
  1093.     putstring(outline[i]);
  1094.     }
  1095.   clearline();
  1096. }
  1097.  
  1098.  
  1099. /****************************************************************************
  1100.  
  1101.   splitline
  1102.  
  1103.   Splits inchrline at the last word break (bunch of consecutive blanks).
  1104.   Makes a new line out of the first part and prints it using
  1105.   printline.  Makes a new line out of the second part and returns.
  1106.  
  1107. ****************************************************************************/
  1108.  
  1109. void splitline()
  1110. {
  1111.   int i,gotspace,lastspace,len1,len2;
  1112.   inchr *part1,*part2;
  1113.  
  1114.   part1 = (inchr*)myalloc(sizeof(inchr)*(inchrlinelen+1));
  1115.   part2 = (inchr*)myalloc(sizeof(inchr)*(inchrlinelen+1));
  1116.   gotspace = 0;
  1117.   for (i=inchrlinelen-1;i>=0;i--) {
  1118.     if (!gotspace && inchrline[i]==' ') {
  1119.       gotspace = 1;
  1120.       lastspace = i;
  1121.       }
  1122.     if (gotspace && inchrline[i]!=' ') {
  1123.       break;
  1124.       }
  1125.     }
  1126.   len1 = i+1;
  1127.   len2 = inchrlinelen-lastspace-1;
  1128.   for (i=0;i<len1;i++) {
  1129.     part1[i] = inchrline[i];
  1130.     }
  1131.   for (i=0;i<len2;i++) {
  1132.     part2[i] = inchrline[lastspace+1+i];
  1133.     }
  1134.   clearline();
  1135.   for (i=0;i<len1;i++) {
  1136.     addchar(part1[i]);
  1137.     }
  1138.   printline();
  1139.   for (i=0;i<len2;i++) {
  1140.     addchar(part2[i]);
  1141.     }
  1142.   free(part1);
  1143.   free(part2);
  1144. }
  1145.  
  1146.  
  1147. /****************************************************************************
  1148.  
  1149.   handlemapping
  1150.  
  1151.   Given an input character (type inchr), executes re-mapping commands
  1152.   read from control files.  Returns re-mapped character (inchr).
  1153.  
  1154. ****************************************************************************/
  1155.  
  1156. inchr handlemapping(c)
  1157. inchr c;
  1158. {
  1159.   comnode *cmptr;
  1160.  
  1161.   cmptr=commandlist;
  1162.   while (cmptr!=NULL) {
  1163.     if (cmptr->thecommand ?
  1164.       (c >= cmptr->rangelo && c <= cmptr->rangehi) : 0) {
  1165.       c += cmptr->offset;
  1166.       while(cmptr!=NULL ? cmptr->thecommand : 0) {
  1167.         cmptr=cmptr->next;
  1168.         }
  1169.       }
  1170.     else {
  1171.       cmptr=cmptr->next;
  1172.       }
  1173.     }
  1174.   return c;
  1175. }
  1176.  
  1177.  
  1178. /****************************************************************************
  1179.  
  1180.   main
  1181.  
  1182.   The main program, of course.
  1183.   Reads characters 1 by 1 from stdin, and makes lines out of them using
  1184.   addchar. Handles line breaking, (which accounts for most of the
  1185.   complexity in this function).
  1186.  
  1187. ****************************************************************************/
  1188.  
  1189. int main(argc,argv)
  1190. int argc;
  1191. char *argv[];
  1192. {
  1193.   inchr c,c2;
  1194.   int i;
  1195.   int last_was_eol_flag;
  1196. /*---------------------------------------------------------------------------
  1197.   wordbreakmode:
  1198.     -1: /^$/ and blanks are to be absorbed (when line break was forced
  1199.       by a blank or character larger than outlinelenlimit)
  1200.     0: /^ *$/ and blanks are not to be absorbed
  1201.     1: /[^ ]$/ no word break yet
  1202.     2: /[^ ]  *$/
  1203.     3: /[^ ]$/ had a word break
  1204. ---------------------------------------------------------------------------*/
  1205.   int wordbreakmode;
  1206.   int char_not_added;
  1207.  
  1208.   getparams(argc,argv);
  1209.   readcontrolfiles();
  1210.   readfont();
  1211.   linealloc();
  1212.  
  1213.   wordbreakmode = 0;
  1214.   last_was_eol_flag = 0;
  1215.  
  1216.   while ((c = getchar())!=EOF) {
  1217.  
  1218.     if (c=='\n'&¶graphflag&&!last_was_eol_flag) {
  1219.       ungetc(c2 = getchar(),stdin);
  1220.       c = ((isascii(c2)&&isspace(c2))?'\n':' ');
  1221.       }
  1222.     last_was_eol_flag = (isascii(c)&&isspace(c)&&c!='\t'&&c!=' ');
  1223.  
  1224.     if (deutschflag) {
  1225.       if (c>='[' && c<=']') {
  1226.         c += -255-'[';
  1227.         }
  1228.       else if (c >='{' && c <= '~') {
  1229.         c += -252-'{';
  1230.         }
  1231.       }
  1232.  
  1233.     c = handlemapping(c);
  1234.  
  1235.     if (isascii(c)&&isspace(c)) {
  1236.       c = (c=='\t'||c==' ') ? ' ' : '\n';
  1237.       }
  1238.  
  1239.     if ((c>'\0' && c<' ' && c!='\n') || c==127) continue;
  1240.  
  1241. /*
  1242.   Note: The following code is complex and thoroughly tested.
  1243.   Be careful when modifying!
  1244. */
  1245.  
  1246.     do {
  1247.       char_not_added = 0;
  1248.  
  1249.       if (wordbreakmode== -1) {
  1250.         if (c==' ') {
  1251.           break;
  1252.           }
  1253.         else if (c=='\n') {
  1254.           wordbreakmode = 0;
  1255.           break;
  1256.           }
  1257.         wordbreakmode = 0;
  1258.         }
  1259.  
  1260.       if (c=='\n') {
  1261.         printline();
  1262.         wordbreakmode = 0;
  1263.         }
  1264.  
  1265.       else if (addchar(c)) {
  1266.         if (c!=' ') {
  1267.           wordbreakmode = (wordbreakmode>=2)?3:1;
  1268.           }
  1269.         else {
  1270.           wordbreakmode = (wordbreakmode>0)?2:0;
  1271.           }
  1272.         }
  1273.  
  1274.       else if (outlinelen==0) {
  1275.         for (i=0;i<charheight;i++) {
  1276.           if (right2left && outputwidth>1) {
  1277.             putstring(currchar[i]+MYSTRLEN(currchar[i])-outlinelenlimit);
  1278.             }
  1279.           else {
  1280.             putstring(currchar[i]);
  1281.             }
  1282.           }
  1283.         wordbreakmode = -1;
  1284.         }
  1285.  
  1286.       else if (c==' ') {
  1287.         if (wordbreakmode==2) {
  1288.           splitline();
  1289.           }
  1290.         else {
  1291.           printline();
  1292.           }
  1293.         wordbreakmode = -1;
  1294.         }
  1295.  
  1296.       else {
  1297.         if (wordbreakmode>=2) {
  1298.           splitline();
  1299.           }
  1300.         else {
  1301.           printline();
  1302.           }
  1303.         wordbreakmode = (wordbreakmode==3)?1:0;
  1304.         char_not_added = 1;
  1305.         }
  1306.  
  1307.       } while (char_not_added);
  1308.     }
  1309.  
  1310.   if (outlinelen!=0) {
  1311.     printline();
  1312.     }
  1313.   exit(0);
  1314. }
  1315.